//
//  HYOnlineTopCell.m
//  HaiYue
//
//  Created by skyline on 2018/2/5.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "HYOnlineTopCell.h"
#import "HYCycleScroll.h"
#import "HYOnlineBarCell.h"
@interface HYOnlineTopCell()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) HYCycleScroll *cycle;
@property (nonatomic, strong) UICollectionView *collectionView;

@end
@implementation HYOnlineTopCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.separatorInset = UIEdgeInsetsMake(0, SCREEN_WIDTH, 0, 0);
        self.cycle.hidden = NO;
        self.collectionView.hidden = NO;
    }
    return self;
}

- (HYCycleScroll *)cycle
{
    if (!_cycle)
    {
        _cycle = [[HYCycleScroll alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HYCycleScrollHeight)];
        [self addSubview:_cycle];
        [_cycle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self);
            make.left.equalTo(self);
            make.size.mas_equalTo(self.cycle.bounds.size);
        }];
    }
    return _cycle;
}

- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:[self layout]];
        [_collectionView registerClass:[HYOnlineBarCell class] forCellWithReuseIdentifier:@"HYOnlineBarCell"];
        _collectionView.backgroundColor = [UIColor redColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.bounces = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.hidden = YES;
        [self addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).with.offset(HYCycleScrollHeight + GAP * 2 - 2.5 * 2);
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.height.mas_offset(HYOnlineBarCellHeight);
        }];
    }
    return _collectionView;
}

- (UICollectionViewFlowLayout *)layout
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 0;
    layout.itemSize = CGSizeMake(SCREEN_WIDTH / 4.0, HYOnlineBarCellHeight);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    return layout;
}

- (CGFloat)H
{
    return HYCycleScrollHeight;
}

- (void)setTopgames:(NSMutableArray<HYTopGameModel *> *)topgames
{
    if (_topgames != topgames)
    {
        _topgames = topgames;
        self.cycle.topGames = topgames;
    }
}

- (void)setBookgames:(NSMutableArray<HYBookGameModel *> *)bookgames
{
    if (_bookgames != bookgames)
    {
        _bookgames = bookgames;

        self.collectionView.bounds = ([_bookgames count] > 0)?CGRectMake(0, 0, SCREEN_WIDTH, HYOnlineBarCellHeight):CGRectZero;
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_offset(self.collectionView.bounds.size.height);
        }];
        [self.collectionView reloadData];
    }
}

#pragma mark CollectionViewDelegate&DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.bookgames count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* cellId = @"HYOnlineBarCell";
    HYOnlineBarCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    cell.model = self.bookgames[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    if (self.delegate && [self.delegate respondsToSelector:@selector(methodForHYOnlineTopCellSelectedGame:)])
//    {
//        [self.delegate methodForHYOnlineTopCellSelectedGame:indexPath.row];
//    }
}

//#pragma mark - HYCycleScrollDelegate
//- (void)methodForHYCycleScrollDidSelect:(NSInteger)index
//{
//    [CCNotice post:CCNoticeNameCycleScrollClick obj:nil info:@{@"index":@(index)}];
//}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
